<template>
    <div >
        <el-form :inline="true" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
            <!-- 搜索框-->
       
         <el-form-item>
            <el-button :type="timeType == 1 ? 'success' : 'info'" @click="changeTime(1)" size="small">按年</el-button>
            <el-button :type="timeType == 2 ? 'success' : 'info'" @click="changeTime(2)" size="small">按月</el-button>
         </el-form-item>
       <el-form-item>
        <el-date-picker v-if="timeType == 1"
          v-model="queryYear"
          type="year"
            value-format="yyyy"
          placeholder="选择年">
        </el-date-picker>
      </el-form-item>
        <el-form-item>
            <el-date-picker v-if="timeType == 2"
              v-model="queryMonth"
              type="month"
              value-format="yyyy-MM"
              placeholder="选择月">
            </el-date-picker>
        </el-form-item>
             <el-form-item prop="id">
            <GridSelect
                    title="选择商家"
                    labelName = 'shopName'
                    labelValue = 'id'
                    :value = "searchForm.id"
                    :limit="1"
                    size="small"
                    @getValue='(value) => {searchForm.id=value}'
                    :columns="[
                    {
                      prop: 'id',
                      label: '主键'
                    },
                    {
                      prop: 'shopName',
                      label: '商家名称'
                    },
                    {
                      prop: 'contacts',
                      label: '联系人'
                    },
                    {
                      prop: 'contactsPhone',
                      label: '联系人电话'
                    },
                    {
                      prop: 'delFlags',
                      label: '是否注销'
                    }
                    ]"
                    :searchs="[
                    {
                      prop: 'shopName',
                      label: '商家名称'
                    }
                    ]"
                    dataListUrl="/tshopinfo/tShopInfo/list"
                    entityBeanName="tShopInfo"
                    queryEntityUrl="/tshopinfo/tShopInfo/queryById">
                  </GridSelect>
         </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="refreshList()" size="small">查询</el-button>
            <el-button @click="resetSearch()" size="small">重置</el-button>
          </el-form-item>
      </el-form>
      <div style="display:flex;">
       <div style="width:40%">
    <el-table
      :data="dataList"
      @current-change="handleCurrentChange"
      highlight-current-row
      border
      size="medium"
      @selection-change="selectionChangeHandle"
      @sort-change="sortChangeHandle"
      v-loading="loading"
      class="table">
      <el-table-column
        prop="shopName"
        show-overflow-tooltip
        label="
        商家名称">
      </el-table-column>
    <el-table-column
        prop="nameType"
        show-overflow-tooltip
        label="商家类型 ">
      </el-table-column>
    <el-table-column
        prop="concernsNumber"
        show-overflow-tooltip
        label="关注量">
      </el-table-column>
      <el-table-column
        prop="callsNumber"
        show-overflow-tooltip
        label="拨打量">
      </el-table-column>
      </el-table>
      <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNo"
      :page-sizes="[10, 15, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      background
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
      </div>
     <div id="myChart" :style="{width: '100%', height: '760px'}"></div>
    </div>
 </div>
    
</template>

<script>
  import TCallRecordForm from './TCallRecordForm'
  import GridSelect from '@/components/gridSelect'
  // 引入Echarts
  import * as echarts from "echarts";
  export default {
    data () {
      return {
        searchForm: {
         id:'',//商家ID
         queryDate:'',
         reportType:''
        },
        timeType: 2, 
        queryMonth:this.dateFormat(2),
        queryYear:'',
        dataList: [],
        pageNo: 1,
        pageSize: 15,
        total: 0,
        orderBy: '',
        dataListSelections: [],
        isSearchCollapse: false,
        isImportCollapse: false,
        loading: false,
        shopName:'',
        nameType:'',
        concernsNumber:'',
        callsNumber:'',
        currentRow:null,
        evdate:[],
        concernsNumber:[],
        callsNumber:[]
      }
    },
    mounted() {
      // this.drawLine() 
      
    },
    components: {
      GridSelect,
      TCallRecordForm
    },
    activated () {
     
      this.refreshList()
    },

    methods: {
      dateFormat(type){
     
        var date = new Date()
        var year = date.getFullYear()
        var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth()+ 1
        // var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
        if(type == 2){
          return year + '-' +month
        }else{
          console.log(year+'');
          return year+''
        }
      },
      changeTime (timeType){
         console.log(timeType,222);
        let strdata = this.dateFormat(timeType);
        if (timeType == 1){
          console.log(strdata,1111);
          this.queryYear =  strdata
          console.log(this.queryYear,1111);
        } else {
            this.queryMonth = strdata
        }
        this.timeType = timeType
      },
      drawLine() {
      // 基于准备好的dom，初始化echarts实例
      let myChart = echarts.init(document.getElementById('myChart'))
      myChart.setOption({
        title: {
          text: this.currentRow.shopName,
          left:'50%'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['关注量', '拨打量'],
          left:'10%'
        },
        toolbox: {
          show: true,
          feature: {
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar'] },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        calculable: true,
        xAxis: [
          {
            type: 'category',
            // prettier-ignore
            data: this.evdate
          }
        ],
        yAxis: [
          {
            type: 'value',
            // nameLocation: 'end',
            // data: [0,30,60,90,120,150,180,210,240,270,300]
          }
        ],
        series: [
          {
            name: '关注量',
            type: 'bar',
            data: this.concernsNumber,
            markPoint: {
              data: [
                { type: 'max', name: 'Max' },
                { type: 'min', name: 'Min' }
              ]
            },
            markLine: {
              data: [{ type: 'average', name: 'Avg' }]
            }
          },
          {
            name: '拨打量',
            type: 'bar',
            data: this.callsNumber,
            markPoint: {
              data: [
                { name: 'Max', value: 182.2, xAxis: 7, yAxis: 183 },
                { name: 'Min', value: 2.3, xAxis: 11, yAxis: 3 }
              ]
            },
            markLine: {
              data: [{ type: 'average', name: 'Avg' }]
            }
          }
        ]
      })
    },
      // 获取数据列表
      refreshList () {
        this.loading = true

        if (this.timeType == 1){
          this.searchForm.queryDate = this.queryYear + '-01-01'
        } else {
            this.searchForm.queryDate = this.queryMonth + '-01'
        }
        this.searchForm.reportType = this.timeType
        this.$http({
          url: '/cellrecord/tCallRecord/queryFollowAndCallReport',
          method: 'POST',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            ...this.searchForm
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.loading = false
            console.log(data.data)
            this.dataList = data.data.dataList
            this.total = data.data.count 
            this.currentRow = this.dataList[0];
            this.evdate = []
            this.concernsNumber=[],
            this.callsNumber=[]
             data.data.dataReport.forEach(e => {
                  this.evdate.push(e.evdate)
                   this.concernsNumber.push(e.concernsNumber),
                  this.callsNumber.push(e.callsNumber)
             });
            this.drawLine()
            
          }
        })
      },
      handleCurrentChange(val) {
        this.currentRow = val;
        this.queryReportByShopId(val.id);
       
      },
      queryReportByShopId (id) {
        let queryDate = ''
        if (this.timeType == 1){
           queryDate = this.queryYear + '-01-01'
        } else {
          queryDate = this.queryMonth + '-01'
        }
        this.searchForm.reportType = this.timeType
        this.$http({
          url: '/cellrecord/tCallRecord/queryFollowAndCallReportByShopId',
          method: 'POST',
          params: {
            'id': id,
            'reportType': this.timeType, 
            'queryDate': queryDate, 
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.evdate = []
            this.concernsNumber=[],
            this.callsNumber=[]
             data.data.dataReport.forEach(e => {
                  this.evdate.push(e.evdate)
                   this.concernsNumber.push(e.concernsNumber),
                  this.callsNumber.push(e.callsNumber)
             });
              this.drawLine()
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageNo = val
        this.refreshList()
      },
      // 多选
      selectionChangeHandle (val) {
        
        this.dataListSelections = val
      },

    // 排序
      sortChangeHandle (obj) {
        if (obj.order === 'ascending') {
          this.orderBy = obj.prop + ' asc'
        } else if (obj.order === 'descending') {
          this.orderBy = obj.prop + ' desc'
        } else {
          this.orderBy = ''
        }
        this.refreshList()
      },
      // 新增
      add () {
        this.$refs.tCallRecordForm.init('add', '')
      },
      // 修改
      edit (id) {
        id = id || this.dataListSelections.map(item => {
          return item.id
        })[0]
        this.$refs.tCallRecordForm.init('edit', id)
      },
      // 查看
      view (id) {
        this.$refs.tCallRecordForm.init('view', id)
      },
      // 删除
      del (id) {
        let ids = id || this.dataListSelections.map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/cellrecord/tCallRecord/delete',
            method: 'delete',
            params: {'ids': ids}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      },
      // 导入成功
      uploadSuccess (res, file) {
        if (res.success) {
          this.$message.success({dangerouslyUseHTMLString: true,
            message: res.msg})
        } else {
          this.$message.error(res.msg)
        }
      },
      // 下载模板
      downloadTpl () {
        this.$utils.download('/cellrecord/tCallRecord/import/template')
      },
      exportExcel () {
        this.$utils.download('/cellrecord/tCallRecord/export')
      },
      resetSearch () {
        this.searchForm.id = ''
        this.timeType = '2'
        this.queryMonth = this.dateFormat(2)
        this.$refs.searchForm.resetFields()
        this.refreshList()
      }
    }
  }
</script>